package org.sanguine.study.hometasks.first.task.a;

import java.util.regex.Pattern;

import org.sanguine.study.hometasks.first.Runner;

public class TaskA5 extends CommonWordTask {
	private int countOfLatinWords;
	private int countOfEqualSoundsWords;
	

	public static void main(String[] args) {
		Runner runner = new TaskA5();
		runner.run(args);
	}

	@Override
	public void doTask() {
		if (in == null || out == null || numberOfItems <= 0) return;
		readItems();
		if (items != null) countLatinWordsAndSounds();
		printResult();
	}

	private void countLatinWordsAndSounds() {
		for (String s : items) {
			if (isOnlyLatin(s)) {
				countOfLatinWords++;
				if (isEqualsSounds(s)) {
					countOfEqualSoundsWords++;
//					System.err.println(s);
				}
			}
		}
		
	}

	private boolean isEqualsSounds(String s) {
		String vowels = "AEIOU";
		
		int countOfVowels = 0;
		int countOfConsonant = 0;
		
		for (Character c : s.toCharArray()) {
			if (Character.isAlphabetic(c)) {
				if (vowels.indexOf(Character.toUpperCase(c)) != -1) countOfVowels++;
				else countOfConsonant++;
			}
		}
		return countOfVowels == countOfConsonant;
	}

	private boolean isOnlyLatin(String s) {
		return Pattern.matches("^[a-zA-Z]+$", s);
	}

	private void printResult() {
		out.printf("Latin words: %d, equals sounds' words: %d\n", countOfLatinWords, countOfEqualSoundsWords);
	}

}
